package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.erp.dto.ProcessDto;
import com.yc.sole.erp.entity.Process;
import com.yc.sole.erp.mapper.ProcessMapper;
import com.yc.sole.erp.request.ProcessQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-06
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ProcessRepository extends ServiceImpl<ProcessMapper, Process> {

    /**
     * 新增
     *
     * @param processDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveProcess(ProcessDto processDto) {
        Process entity = new Process();
        BeanUtil.copyProperties(processDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param processDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProcessById(ProcessDto processDto) {
        Process entity = new Process();
        BeanUtil.copyProperties(processDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcessByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<ProcessDto> selectProcessByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), ProcessDto.class);
    }

    /**
     * 查询所有可用工序
     *
     * @return RestResult
     */
    public List<ProcessDto> selectAllProcess() {
        LambdaQueryWrapper<Process> wrapper = Wrappers.<Process>lambdaQuery().eq(Process::getState, CommonState.ENABLE);
        return BeanUtil.copyToList(this.list(wrapper), ProcessDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public ProcessDto selectProcessById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), ProcessDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<ProcessDto> selectProcessPage(Paging page, ProcessQueryReq queryReq) {
        LambdaQueryWrapper<Process> queryWrapper = Wrappers.<Process>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(queryReq.getCode()), Process::getCode, queryReq.getCode())
                .like(CharSequenceUtil.isNotBlank(queryReq.getName()), Process::getName, queryReq.getName())
                .eq(Objects.nonNull(queryReq.getState()), Process::getState, queryReq.getState())
                .orderByDesc(Process::getId)
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()), w ->
                        w.like(Process::getCode, queryReq.getKeyword())
                                .or().like(Process::getName, queryReq.getKeyword())
                );
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), ProcessDto.class);
    }
}
